package com.lehoon.tomato.controller;


import com.lehoon.tomato.model.Bedroom;
import com.lehoon.tomato.model.City;
import com.lehoon.tomato.model.Region;
import com.lehoon.tomato.model.School;
import com.lehoon.tomato.model.common.Result;
import com.lehoon.tomato.model.common.ValidateException;
import com.lehoon.tomato.model.vo.CityVo;
import com.lehoon.tomato.model.vo.SchoolVo;
import com.lehoon.tomato.service.CityService;
import com.lehoon.tomato.service.SchoolService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 *学校区域
 * @author wang
 * @since 2020-09-23
 */
@RestController
@RequestMapping("/unauth/school")
@Api(description = "学校模块接口")
public class SchoolController {

    @Resource
    private CityService cityService;

    @Resource
    private SchoolService schoolService;

    @GetMapping("/getCityList")
    @ApiOperation(value = "获取城市列表")
    public Result<List<CityVo>> getCityList(){
        List<City> cityList = cityService.getCityList();
        List<CityVo> voList = cityList.stream().map(v->{
            CityVo vo  = new CityVo();
            BeanUtils.copyProperties(v,vo);
            return vo;
        }).collect(Collectors.toList());
        return Result.successResult(voList);
    }

    @GetMapping("/getSchoolList")
    @ApiOperation(value = "根据条件获取城市列表")
    public Result<List<SchoolVo>> getSchoolList(SchoolVo schoolVo){
        List<School> schoolList = schoolService.querySchool(schoolVo);
        List<SchoolVo> voList = schoolList.stream().map(v->{
            SchoolVo vo  = new SchoolVo();
            BeanUtils.copyProperties(v,vo);
            return vo;
        }).collect(Collectors.toList());
        return Result.successResult(voList);
    }

    @GetMapping("/getSchoolRegion")
    @ApiOperation(value = "获取学校区域")
    @ApiImplicitParam(name="schoolId",value="学校id",dataType="int", paramType = "query")
    public Result<List<Region>>  getSchoolRegion(Integer schoolId){
        List<Region> regionList = schoolService.getSchoolRegion(schoolId);
        return Result.successResult(regionList);
    }

    @GetMapping("/getSchoolBedroom")
    @ApiOperation(value = "获取学校宿舍楼")
    @ApiImplicitParams({
            @ApiImplicitParam(name="schoolId",value="学校id",dataType="int", paramType = "query")
    })
    public Result<List<Bedroom>> getSchoolBedroom(Integer schoolId,Integer regionId){
        if (schoolId==null){
            throw new ValidateException("学校id不能为空");
        }
        List<Bedroom> list = schoolService.getSchoolBedroom(schoolId,regionId);
        return Result.successResult(list);
    }

}

